﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp0
{
    public partial class shang_dan_1qveUserControl1 : UserControl
    {
        public shang_dan_1qveUserControl1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void shang_dan_overUserControl1_Load(object sender, EventArgs e)
        {
            Table_qve();
        }
        private void Table_qve() //待确认
        {
            dataGridView1.Rows.Clear();
            string sql1 = "select * from OrderM where Ostate='待确认'";
            Dao dao = new Dao();
            IDataReader dr1 = dao.read(sql1);
            while (dr1.Read())
            {
                string a, b, c, d, e="", f, g, h, i;
                a = dr1["Oid"].ToString();
                b = dr1["Mid"].ToString();
                string sql2 = $"select * from Merchandise where Mid={b}";
                string aa = "";
                IDataReader dr2 = dao.read(sql2);
                while (dr2.Read())
                {
                    aa = dr2["Mname"].ToString();
                    e = dr2["Mprince"].ToString();
                }
                dr2.Close();
                c = aa;
                d = dr1["Onum"].ToString();
                
                f = dr1["Cid"].ToString();
                string sql3 = $"select * from Customer where Cid={f}";
                string bb = "";
                IDataReader dr3 = dao.read(sql3);
                while (dr3.Read())
                {
                    bb = dr3["Cname"].ToString();
                }
                dr3.Close();
                g = bb;
                h = dr1["Oplace"].ToString();
                i = dr1["Otime"].ToString();
                string[] str = { a, b, c, d, e, f, g, h, i };
                dataGridView1.Rows.Add(str);
            }
            dr1.Close();//关闭连接 
        }
        private void button4_Click(object sender, EventArgs e)
        {//商品状态 待确认-》待送货
            string Oid = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//获取编号
            string sql1 = $"update OrderM set Ostate='待送货',Oout='{DateTime.Now.ToString("yyyy-MM-dd")}' where Oid='{Oid}'";
            Dao dao = new Dao();
            int n = dao.Execute(sql1);

            if (n > 0)
            {//售出时间
                MessageBox.Show("状态改变");
            }
            Table_qve();
        }

        private void button1_Click(object sender, EventArgs e)
        {//按订单编号查询，订单编号在textBox1
            Oid_search();
        }
        private void Oid_search()
        {
            dataGridView1.Rows.Clear();
            string sql1 = $"select * from OrderM where Ostate='待确认'and Oid like '{textBox1.Text}%'";
            Dao dao = new Dao();
            IDataReader dr1 = dao.read(sql1);
            while (dr1.Read())
            {
                string a, b, c, d, e = "", f, g, h, i;
                a = dr1["Oid"].ToString();
                b = dr1["Mid"].ToString();
                string sql2 = $"select * from Merchandise where Mid={b}";
                string aa = "";
                IDataReader dr2 = dao.read(sql2);
                while (dr2.Read())
                {
                    aa = dr2["Mname"].ToString();
                    e = dr2["Mprince"].ToString();
                }
                dr2.Close();
                c = aa;
                d = dr1["Onum"].ToString();

                f = dr1["Cid"].ToString();
                string sql3 = $"select * from Customer where Cid={f}";
                string bb = "";
                IDataReader dr3 = dao.read(sql3);
                while (dr3.Read())
                {
                    bb = dr3["Cname"].ToString();
                }
                dr3.Close();
                g = bb;
                h = dr1["Oplace"].ToString();
                i = dr1["Otime"].ToString();
                string[] str = { a, b, c, d, e, f, g, h, i };
                dataGridView1.Rows.Add(str);
            }
            dr1.Close();//关闭连接 

        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {//删除订单：根据订单编号
            try
            {
                string Oid = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//获取编号
                DialogResult dr = MessageBox.Show("确认删除" + Oid + dataGridView1.SelectedRows[0].Cells[1].Value.ToString(), "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    string sql1 = $"delete from OrderM where Oid='{Oid}'";
                    Dao dao = new Dao();
                    if (dao.Execute(sql1) > 0)
                    {
                        MessageBox.Show("删除成功");
                        Table_qve();//删除刷新
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }

                }
                Table_qve();
            }
            catch
            {
                MessageBox.Show("请选择删除商品");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {//显示的所有   待确认  订单
            Table_qve();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
